#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

import platform

Import('AgentEnv')
env = AgentEnv.Clone()

if platform.system() != 'Windows':
    env.Append(CCFLAGS = ['-Wno-return-type', '-fexceptions', '-Wno-unused-variable'])


##########################################################################
# Rules to generate buildinfo.cc
##########################################################################

# Dependencies on external libraries first
buildinfo_dep_libs = [
    '#build/lib/' + env['LIBPREFIX'] + 'http' + env['LIBSUFFIX'],
    '#build/lib/' + env['LIBPREFIX'] + 'http_parser' + env['LIBSUFFIX'],
    '#build/lib/' + env['LIBPREFIX'] + 'pugixml' + env['LIBSUFFIX'],
    '#build/lib/' + env['LIBPREFIX'] + 'sandesh' + env['LIBSUFFIX'],
    '#build/lib/' + env['LIBPREFIX'] + 'sandeshflow' + env['LIBSUFFIX'],
    '#build/lib/' + env['LIBPREFIX'] + 'sandeshvns' + env['LIBSUFFIX'],
]

def MapBuildCmnLib(list):
    return [File('../../../' + x).abspath for x in list]

# Common contrail libraries
buildinfo_dep_libs += MapBuildCmnLib([
    'base/' + env['LIBPREFIX'] + 'base' + env['LIBSUFFIX'],
    'base/' + env['LIBPREFIX'] + 'cpuinfo' + env['LIBSUFFIX'],
    'db/' + env['LIBPREFIX'] + 'db' + env['LIBSUFFIX'],
    'dns/bind/' + env['LIBPREFIX'] + 'bind_interface' + env['LIBSUFFIX'],
    'ifmap/' + env['LIBPREFIX'] + 'ifmap_agent' + env['LIBSUFFIX'],
    'ifmap/' + env['LIBPREFIX'] + 'ifmap_common' + env['LIBSUFFIX'],
    'io/' + env['LIBPREFIX'] + 'io' + env['LIBSUFFIX'],
    'ksync/' + env['LIBPREFIX'] + 'ksync' + env['LIBSUFFIX'],
    'net/' + env['LIBPREFIX'] + 'net' + env['LIBSUFFIX'],
    'route/' + env['LIBPREFIX'] + 'route' + env['LIBSUFFIX'],
    'schema/' + env['LIBPREFIX'] + 'bgp_schema' + env['LIBSUFFIX'],
    'schema/' + env['LIBPREFIX'] + 'ifmap_vnc' + env['LIBSUFFIX'],
    'ifmap/client/' + env['LIBPREFIX'] + 'ifmapio' + env['LIBSUFFIX'],
    'schema/' + env['LIBPREFIX'] + 'xmpp_unicast' + env['LIBSUFFIX'],
    'schema/' + env['LIBPREFIX'] + 'xmpp_multicast' + env['LIBSUFFIX'],
    'schema/' + env['LIBPREFIX'] + 'xmpp_mvpn' + env['LIBSUFFIX'],
    'vrouter/utils/' + env['LIBPREFIX'] + 'vrutil' + env['LIBSUFFIX'],
    'xml/' + env['LIBPREFIX'] + 'xml' + env['LIBSUFFIX'],
    'xmpp/' + env['LIBPREFIX'] + 'xmpp' + env['LIBSUFFIX'],
])

def MapBuildAgentLib(list):
    return [File('../' + x).abspath for x in list]

# agent libraries
buildinfo_dep_libs += MapBuildAgentLib([
    'cfg/' + env['LIBPREFIX'] + 'cfg' + env['LIBSUFFIX'],
    'cmn/' + env['LIBPREFIX'] + 'vnswcmn' + env['LIBSUFFIX'],
    'controller/' + env['LIBPREFIX'] + 'vnswctrl' + env['LIBSUFFIX'],
    'filter/' + env['LIBPREFIX'] + 'filter' + env['LIBSUFFIX'],
    'kstate/' + env['LIBPREFIX'] + 'kstate' + env['LIBSUFFIX'],
    'vrouter/ksync/' + env['LIBPREFIX'] + 'vnswksync' + env['LIBSUFFIX'],
    'port_ipc/' + env['LIBPREFIX'] + 'port_ipc' + env['LIBSUFFIX'],
    'oper/' + env['LIBPREFIX'] + 'vnswoperdb' + env['LIBSUFFIX'],
    'pkt/' + env['LIBPREFIX'] + 'pkt' + env['LIBSUFFIX'],
    'diag/' + env['LIBPREFIX'] + 'diag' + env['LIBSUFFIX'],
    'services/' + env['LIBPREFIX'] + 'agent_services' + env['LIBSUFFIX'],
    'services/multicast/gmp_map/' + env['LIBPREFIX'] + 'gmp_map' + env['LIBSUFFIX'],
    'services/multicast/grpmgmt/' + env['LIBPREFIX'] + 'grpmgmt' + env['LIBSUFFIX'],
    'services/multicast/stubs/' + env['LIBPREFIX'] + 'gmp_utils' + env['LIBSUFFIX'],
    'services/multicast/agent_map/' + env['LIBPREFIX'] + 'agent_map' + env['LIBSUFFIX'],
    'uve/' + env['LIBPREFIX'] + 'uve' + env['LIBSUFFIX'],
    'uve/' + env['LIBPREFIX'] + 'statsuve' + env['LIBSUFFIX'],
    'vrouter/flow_stats/' + env['LIBPREFIX'] + 'flowstats' + env['LIBSUFFIX'],
    'vrouter/stats_collector/' + env['LIBPREFIX'] + 'statscollector' + env['LIBSUFFIX'],
])

env.GenerateBuildInfoCode(
    target = ['buildinfo.h', 'buildinfo.cc'],
    source = [],
    path=str(Dir('.').abspath))
build_obj = [env.Object('buildinfo.cc')]

# Add contrail-vrouter-agent specific libraries
env.Prepend(LIBS = AgentEnv['CONTRAIL_LIBS'])

if platform.system() != 'Windows':
    # Whole-arhive flag causes the library to be included in the resulting output
    # even though there may not be any calls requiring its presence. We need this
    # for kstate library so that the HTML file for kstate  is listed in agent
    # introspect page
    env.Prepend(LINKFLAGS = ['-Wl,--whole-archive', '-rdynamic', '-lkstate', '-ldiag',
                             '-Wl,--no-whole-archive'])

#Add -Wno-return-type to ignore errors from thrift generated code in
#InstanceService.h
if platform.system() == 'FreeBSD':
    platform_dependent = 'freebsd/pkt0_interface.cc'
elif platform.system() == 'Linux':
    platform_dependent = 'linux/pkt0_interface.cc'
elif platform.system() == 'Windows':
    env.Append(LIBS = ['version'])
    platform_dependent = 'windows/pkt0_interface.cc'


contrail_vrouter_agent = env.Program(target = 'contrail-vrouter-agent',
                     source = ['contrail_agent_init.cc',
                               'main.cc',
                               'pkt0_interface_base.cc',
                                buildinfo_dep_libs,
                                build_obj,
                                platform_dependent])

env.Alias('contrail-vrouter-agent', contrail_vrouter_agent)
env.Default(contrail_vrouter_agent)

if platform.system() == 'Windows':
    #Use WiX for MSI generation
    agent_msi = env.WiX('contrail-vrouter-agent.msi', ['windows/msi/agent_msi.wxs'])
    env.Append(WIXCANDLEFLAGS = ['-doptimization=' + env['OPT']])
    if env['OPT'] == 'debug':
        env.Append(WIXCANDLEFLAGS = ['-dTbbDll=tbb_debug.dll'])
        env.Append(WIXCANDLEFLAGS = ['-dLog4CPlusDll=log4cplusD.dll'])
    elif env['OPT'] == 'production':
        env.Append(WIXCANDLEFLAGS = ['-dTbbDll=tbb.dll'])
        env.Append(WIXCANDLEFLAGS = ['-dLog4CPlusDll=log4cplus.dll'])

    env.Append(WIXCANDLEFLAGS = ['-ext', 'WixUtilExtension.dll'])
    env.Append(WIXLIGHTFLAGS = ['-ext', 'WixUtilExtension.dll'])
    env.Depends(agent_msi, [contrail_vrouter_agent])

    env.Alias('contrail-vrouter-agent.msi', agent_msi)
    env.Default(agent_msi)

utils_scripts = [
  'agent_dissector.lua',
  'mpls_dissector.lua',]

# Documentation
doc_files = []
doc_files += env['AGENT_COMMON_DOC_FILES']
doc_files += env['AGENT_UVE_DOC_FILES']
doc_files += env['AGENT_KSYNC_DOC_FILES']
doc_files += env['AGENT_FLOW_DOC_FILES']
doc_files += env['AGENT_STATS_COLLECTOR_DOC_FILES']

# Please update sandesh/common/vns.sandesh on process name change
env.Alias('install', env.Install(env['INSTALL_BIN'], contrail_vrouter_agent))
env.Alias('install', env.Install(env['INSTALL_BIN'], 'contrail-vrouter-agent-health-check.py'))
env.Alias('install', env.Install(env['INSTALL_BIN'], 'contrail_crypt_tunnel_client.py'))
for utils in utils_scripts:
  env.Alias('install', env.Install(env['INSTALL_EXAMPLE'], utils))
env.Alias('install', env.Install(env['INSTALL_CONF'],
          '../contrail-vrouter-agent.conf'))
env.Alias('install',
        env.InstallAs(env['INSTALL_CONF'] +
        '/supervisord_vrouter_files/' + 'contrail-vrouter-agent.ini',
        '../contrail-vrouter-agent.ini'))
env.Alias('install',
        env.InstallAs(env['INSTALL_INITD'] + '/contrail-vrouter-agent',
        '../contrail-vrouter.initd.supervisord'))
env.Alias('install',
        env.InstallAs(env['INSTALL_SYSTEMD'] + '/contrail-vrouter-agent.service',
        '../systemd/contrail-vrouter-agent.service'))
env.Alias('install', env.Install(env['INSTALL_CONF'],
    '../supervisord_vrouter.conf'))
env.Alias('install', env.Install(
    env['INSTALL_MESSAGE_DOC'] + '/contrail-vrouter-agent/',
    doc_files))
